package  
{
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import org.flixel.*;
	
	/**
	 * ...
	 * @author d7ggj
	 */
	public class DarknessBlob extends FlxSprite
	{
		//[Embed(source = '../lib/8_ball.png')] protected var sprite:Class;
		[Embed(source = '../lib/cloud_rotate.png')] protected var darkBall:Class;
		
		public var moveDirection:FlxPoint;
		public var speed:int = 20;
		public var running:Boolean = false;
		public var attacking:Boolean = false;
		public var runDirection:FlxPoint;
		private var maxRunSpeed:int = 200;
		private var maxRunTime:int = 250;
		private var runTimer:Timer;
		private var crystal:LightCrystal;
		private var attackFX:FlxText;
		
		public function DarknessBlob(startX:int, startY:int, lightCrystal:LightCrystal) 
		{
			super(startX, startY);
			
			loadGraphic(darkBall, true, false, 60, 60);
			addAnimation("rotate", [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 10);
			
			moveDirection = new FlxPoint();
			crystal = lightCrystal;
			moveDirection = new FlxPoint();
			getMoveDirection();
			
			runDirection = new FlxPoint();
			runTimer = new Timer(maxRunTime, 1);
			runTimer.addEventListener(TimerEvent.TIMER_COMPLETE, runStopEvent);
		}
		
		override public function update():void
		{
			super.update();
			play("rotate");
			if(!running)
			{
				if (attacking)
				{
					velocity.x = 0;
					velocity.y = 0;
				}
				else
				{
					velocity.x = moveDirection.x * speed;
					velocity.y = moveDirection.y * speed;
				}
			}
			else
			{
				velocity.x = (maxRunSpeed * runDirection.x);
				velocity.y = (maxRunSpeed * runDirection.y);
			}
		}
		
		private function runStopEvent(event:TimerEvent):void
		{
			running = false;
			getMoveDirection();
		}
		
		private function getMoveDirection():void
		{
			moveDirection.x = (crystal.x + crystal.origin.x) - (x + origin.x);
			moveDirection.y = (crystal.y + crystal.origin.y) - (y + origin.y);
			var mag:Number = Math.sqrt((moveDirection.x * moveDirection.x) + (moveDirection.y * moveDirection.y))
			moveDirection.x = moveDirection.x / mag;
			moveDirection.y = moveDirection.y / mag;
		}
		
		public function RunFromLight(shockwave:LightShockwave):void
		{
			//FlxG.log(this.running);
			if(!running)
			{
				attacking = false;
				var lightPos:FlxPoint = new FlxPoint(shockwave.x + shockwave.origin.x, shockwave.y + shockwave.origin.y);
				running = true;
				runDirection.x = (x + origin.x - lightPos.x) / (shockwave.width/2); //175 is how big the shockwave is. We want this to be a fraction of how big that is.
				runDirection.y = (y + origin.y - lightPos.y) / (shockwave.width / 2);
				//FlxG.log(runDirection.x);
				//FlxG.log(runDirection.y);
				runTimer.start();
			}
		}
		
		public function CrystalExplodeDie(crystal:LightCrystal):void
		{
			var distX:Number = x + origin.x - crystal.x + crystal.origin.x;
			var distY:Number = y + origin.y - crystal.y + crystal.origin.y;
			var mag:Number = Math.sqrt((distX * distX) + (distY * distY));
			if (mag <= 175)
				kill();
		}
		
		public function Attack(thatCrystal:LightCrystal):Boolean
		{
			kill();
			return true;
		}
	}

}